using System;
using System.Diagnostics;
using System.Management;

namespace PractBTS.ErrorsCollection.Logic
{
    /// <summary>
    /// Represents and Encapsulation for ManagementObjectt
    /// </summary>
    public class PB_MgmtObjectBase
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="data"></param>
        public PB_MgmtObjectBase(ManagementObject data)
        {
            obj = data;
        }
        /// <summary>
        /// sets the Obejct based on table and filter
        /// </summary>
        /// <param name="table">string</param>
        /// <param name="filter">string</param>
        public void SetObject(string table, string filter)
        {
            try
            {
                string qry = string.Format(PB_MgmtObjectBase.BasicQury, table, filter);
                ManagementObjectSearcher srchr =
                    new ManagementObjectSearcher
                    (PB_MgmtObjectBase.BTSMgmtScope, qry);
                foreach (ManagementObject o in srchr.Get())
                {
                    obj = o;
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
        public static readonly string BTSMgmtScope = "root\\MicrosoftBizTalkServer";
        public static readonly string BasicQury = "Select * from {0} where {1}";

        protected ManagementObject obj = null;

        /// <summary>
        /// Retrieves the contents of ManagementObject property
        /// </summary>
        /// <param name="propName"></param>
        /// <returns>string</returns>
        public string GetProperty(string propName)
        {
            string str = string.Empty;
            try
            {
                str = this.obj.Properties[propName].Value.ToString();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
            return str;
        }
        /// <summary>
        /// Sets the contents of ManagmentObject property
        /// </summary>
        /// <param name="propName"></param>
        /// <param name="val"></param>
        public void SetProperty(string propName, string val)
        {
            try
            {
                this.obj.Properties[propName].Value = val;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
            }
        }
    }
}
